﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using Entity;
using Utilities;

namespace DAL
{
    public class OutProductDataAccess : FreshCmsAccessBase
    {
        #region code CMS

        /// <summary>
        /// Hàm get list cho CMS
        /// </summary>
        /// <param name="isParent"></param>
        /// <param name="name"></param>
        /// <param name="code"></param>
        /// <param name="cate"></param>
        /// <param name="stt"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public List<OutProduct> GetOutProductListByPaging(bool isParent, string name, string code, int cate, int stt, int pageIndex, int pageSize, ref int totalRecord)
        {
            try
            {
                string spName = "";
                if(isParent)
                    spName = "spi_OutProduct_GetListSearchByParentId";
                else
                    spName = "spi_OutProduct_GetListSearch";
                    
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@ProName", name),
                                        new SqlParameter("@ProCode", code),
                                        new SqlParameter("@CatId", cate),
                                        new SqlParameter("@ProStatus", stt),
                                        new SqlParameter("@PageIndex", pageIndex),
                                        new SqlParameter("@PageSize", pageSize),
                                        new SqlParameter("@TotalRecord", totalRecord)
                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                sqlCommand.Parameters["@TotalRecord"].Direction = ParameterDirection.Output;
                var OutProductList = DbHelper.ExecuteSqlList<OutProduct>(sqlCommand);
                if (sqlCommand.Parameters["@TotalRecord"].Value != null)
                {
                    totalRecord = (int)sqlCommand.Parameters["@TotalRecord"].Value;
                }

                return OutProductList;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return new List<OutProduct>();
            }
        }

        /// <summary>
        /// Get chi tiết sản phẩm cho CMS
        /// </summary>
        /// <param name="proId"></param>
        /// <returns></returns>
        public OutProduct GetOutProductById(int proId)
        {
            try
            {
                const string spName = "spi_OutProduct_GetById";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@ProId", proId)
                                    };

                var OutProduct = DbHelper.ExecuteSPList<OutProduct>(spName, sqlParams).SingleOrDefault();
                return OutProduct;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return null;
            }
        }

        public int InsertOutProduct(OutProduct OutProduct)
        {
            try
            {
                const string spName = "spi_OutProduct_Insert";
                var sqlParams = new[]
                                    {

                                        new SqlParameter("@CatId", OutProduct.CatId),
                                        new SqlParameter("@ProName", OutProduct.ProName),
                                        new SqlParameter("@ProCode", OutProduct.ProCode),
                                        new SqlParameter("@ProDesc", OutProduct.ProDesc),
                                        new SqlParameter("@ProDetail1", OutProduct.ProDetail1),
                                        new SqlParameter("@ProDetail2", OutProduct.ProDetail2),
                                        new SqlParameter("@ProDetail3", OutProduct.ProDetail3),
                                        new SqlParameter("@ProImage1", OutProduct.ProImage1),
                                        new SqlParameter("@ProImage2", OutProduct.ProImage2),
                                        new SqlParameter("@ProImage3", OutProduct.ProImage3),
                                        new SqlParameter("@ProImage4", OutProduct.ProImage4),
                                        new SqlParameter("@ProPublishDate", OutProduct.ProPublishDate),
                                        new SqlParameter("@ProOldPrice", OutProduct.ProOldPrice),
                                        new SqlParameter("@ProNewPrice", OutProduct.ProNewPrice),
                                        new SqlParameter("@ProHot", OutProduct.ProHot),
                                        new SqlParameter("@ProNew", OutProduct.ProNew),
                                        new SqlParameter("@ProStatus", OutProduct.ProStatus),
                                        new SqlParameter("@ProIsStock", OutProduct.ProIsStock),
                                        new SqlParameter("@ProPromotion", OutProduct.ProPromotion),
                                        new SqlParameter("@UserId", OutProduct.UserId),
                                        new SqlParameter("@ProId", OutProduct.ProId),

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                sqlCommand.Parameters["@ProId"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@ProId"].Value != null)
                {
                    return (int)sqlCommand.Parameters["@ProId"].Value;
                }

                return 0;


            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        /// <summary>
        /// Hàm update dành cho User, update check theo UserID, đúng UserID tạo ra mới được sửa
        /// </summary>
        /// <param name="OutProduct"></param>
        /// <returns></returns>
        public int UpdateOutProductWidthRole(OutProduct OutProduct)
        {
            try
            {
                int returnVal = 0;
                const string spName = "spi_OutProduct_Update_CheckRole";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@ProId", OutProduct.ProId),
                                        new SqlParameter("@CatId", OutProduct.CatId),
                                        new SqlParameter("@ProName", OutProduct.ProName),
                                        new SqlParameter("@ProCode", OutProduct.ProCode),
                                        new SqlParameter("@ProDesc", OutProduct.ProDesc),
                                        new SqlParameter("@ProDetail1", OutProduct.ProDetail1),
                                        new SqlParameter("@ProDetail2", OutProduct.ProDetail2),
                                        new SqlParameter("@ProDetail3", OutProduct.ProDetail3),
                                        new SqlParameter("@ProImage1", OutProduct.ProImage1),
                                        new SqlParameter("@ProImage2", OutProduct.ProImage2),
                                        new SqlParameter("@ProImage3", OutProduct.ProImage3),
                                        new SqlParameter("@ProImage4", OutProduct.ProImage4),
                                        new SqlParameter("@ProOldPrice", OutProduct.ProOldPrice),
                                        new SqlParameter("@ProNewPrice", OutProduct.ProNewPrice),
                                        new SqlParameter("@ProHot", OutProduct.ProHot),
                                        new SqlParameter("@ProNew", OutProduct.ProNew),
                                        new SqlParameter("@ProStatus", OutProduct.ProStatus),
                                        new SqlParameter("@ProIsStock", OutProduct.ProIsStock),
                                        new SqlParameter("@ProPromotion", OutProduct.ProPromotion),
                                        new SqlParameter("@UserId", OutProduct.UserId),
                                        new SqlParameter("@ReturnValue", returnVal),

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                sqlCommand.Parameters["@ReturnValue"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@ReturnValue"].Value != null)
                {
                    return (int)sqlCommand.Parameters["@ReturnValue"].Value;
                }
                return -1;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -2;
            }
        }

        /// <summary>
        /// Hàm update dành cho Admin, update không check theo UserID
        /// </summary>
        /// <param name="OutProduct"></param>
        /// <returns></returns>
        public int UpdateOutProduct(OutProduct OutProduct)
        {
            try
            {
                const string spName = "spi_OutProduct_Update";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@ProId", OutProduct.ProId)
                                        , new SqlParameter("@CatId", OutProduct.CatId)
                                        , new SqlParameter("@ProName", OutProduct.ProName)
                                        , new SqlParameter("@ProCode", OutProduct.ProCode)
                                        , new SqlParameter("@ProDesc", OutProduct.ProDesc)
                                        , new SqlParameter("@ProDetail1", OutProduct.ProDetail1)
                                        , new SqlParameter("@ProDetail2", OutProduct.ProDetail2)
                                        , new SqlParameter("@ProDetail3", OutProduct.ProDetail3)
                                        , new SqlParameter("@ProImage1", OutProduct.ProImage1)
                                        , new SqlParameter("@ProImage2", OutProduct.ProImage2)
                                        , new SqlParameter("@ProImage3", OutProduct.ProImage3)
                                        , new SqlParameter("@ProImage4", OutProduct.ProImage4)
                                        , new SqlParameter("@ProOldPrice", OutProduct.ProOldPrice)
                                        , new SqlParameter("@ProNewPrice", OutProduct.ProNewPrice)
                                        , new SqlParameter("@ProHot", OutProduct.ProHot)
                                        , new SqlParameter("@ProNew", OutProduct.ProNew)
                                        , new SqlParameter("@ProStatus", OutProduct.ProStatus)
                                        , new SqlParameter("@ProIsStock", OutProduct.ProIsStock)
                                        , new SqlParameter("@ProPromotion", OutProduct.ProPromotion)
                                    };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        /// <summary>
        /// Delete sản phẩm cho Super ADMIN (dùng cho xóa vĩnh viễn)
        /// </summary>
        /// <param name="proId"></param>
        /// <returns></returns>
        public int DeleteOutProduct(int proId)
        {
            try
            {
                const string spName = "spi_OutProduct_Delete";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@ProId", proId)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        /// <summary>
        /// Delete sản phẩm có check đúng tài khoản mới Delete (dùng cho xóa vĩnh viễn)
        /// </summary>
        /// <param name="proId"></param>
        /// <returns></returns>
        public int DeleteOutProductWidthRole(int proId, int userid)
        {
            try
            {
                int returnVal = 0;
                const string spName = "spi_OutProduct_Delete_CheckRole";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@ProId", proId),
                                    new SqlParameter("@UserId", userid),
                                    new SqlParameter("@ReturnValue", returnVal),
                                };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                sqlCommand.Parameters["@ReturnValue"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@ReturnValue"].Value != null)
                    return (int)sqlCommand.Parameters["@ReturnValue"].Value;
                return -1;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -2;
            }
        }

        /// <summary>
        /// Update status sản phẩm với Super ADMIN (dùng cho xóa tạm thời và phục hồi)
        /// </summary>
        /// <param name="proId"></param>
        /// <returns></returns>
        public int UpdateOutProductStatus(int proId, int stt)
        {
            try
            {
                const string spName = "spi_OutProduct_UpdateStatus";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@ProId", proId)
                                    , new SqlParameter("@ProStatus", stt)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        /// <summary>
        /// Update status sản phẩm có check đúng tài khoản mới update (dùng cho xóa tạm thời và phục hồi)
        /// </summary>
        /// <param name="proId"></param>
        /// <returns></returns>
        public int UpdateOutProductStatusWidthRole(int proId, int stt, int userid)
        {
            try
            {
                int returnVal = 0;
                const string spName = "spi_OutProduct_UpdateStatus_CheckRole";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@ProId", proId),
                                    new SqlParameter("@ProStatus", stt),
                                    new SqlParameter("@UserId", userid),
                                    new SqlParameter("@ReturnValue", returnVal),
                                };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                sqlCommand.Parameters["@ReturnValue"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@ReturnValue"].Value != null)
                    return (int)sqlCommand.Parameters["@ReturnValue"].Value;
                return -1;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -2;
            }
        }

        /// <summary>
        /// Hàm update Fast dành cho User, update check theo UserID, đúng UserID tạo ra mới được sửa
        /// </summary>
        /// <param name="OutProduct"></param>
        /// <returns></returns>
        public int UpdateOutProductWidthRole_Fast(OutProduct OutProduct)
        {
            try
            {
                int returnVal = 0;
                const string spName = "spi_OutProduct_UpdateFast_CheckRole";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@ProId", OutProduct.ProId),
                                        new SqlParameter("@ProName", OutProduct.ProName),
                                        new SqlParameter("@ProCode", OutProduct.ProCode),
                                        new SqlParameter("@ProNewPrice", OutProduct.ProNewPrice),
                                        new SqlParameter("@ProStatus", OutProduct.ProStatus),
                                        new SqlParameter("@UserId", OutProduct.UserId),
                                        new SqlParameter("@ReturnValue", returnVal),

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                sqlCommand.Parameters["@ReturnValue"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@ReturnValue"].Value != null)
                    return (int)sqlCommand.Parameters["@ReturnValue"].Value;
                return -1;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -2;
            }
        }

        /// <summary>
        /// Hàm update Fast dành cho Admin, update không check theo UserID
        /// </summary>
        /// <param name="OutProduct"></param>
        /// <returns></returns>
        public int UpdateOutProduct_Fast(OutProduct OutProduct)
        {
            try
            {
                const string spName = "spi_OutProduct_UpdateFast";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@ProId", OutProduct.ProId)
                                        , new SqlParameter("@ProName", OutProduct.ProName)
                                        , new SqlParameter("@ProCode", OutProduct.ProCode)
                                        , new SqlParameter("@ProNewPrice", OutProduct.ProNewPrice)
                                        , new SqlParameter("@ProStatus", OutProduct.ProStatus)
                                    };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

        /// <summary>
        /// Check xem bai viet co dung theo ng viet hay khong, tra ve > 0 la ok
        /// </summary>
        /// <param name="OutProduct"></param>
        /// <returns></returns>
        public int CheckRole(int userid, int proid)
        {
            try
            {
                int temp = 0;
                const string spName = "spi_OutProduct_CheckRole";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@UserId", userid),
                                        new SqlParameter("@ProId", proid),
                                        new SqlParameter("@Return", temp),
                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                sqlCommand.Parameters["@Return"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@Return"].Value != null)
                {
                    return (int)sqlCommand.Parameters["@Return"].Value;
                }

                return 0;


            }
            catch (Exception exp)
            {
                Logger.WriteLog(Logger.LogType.Error,
                                "File: " + (new StackTrace().GetFrame(0).GetMethod().DeclaringType.Name) +
                                " --- Function: " + (new StackTrace().GetFrame(0).GetMethod().Name) +
                                " --- Mess: " + exp.ToString());
                return -1;
            }
        }

		#endregion
    }
}
